package xyz.sunting.tos.passthrough.demo;

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.tws.api.PassThroughManager;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mLogView;
    private StringBuilder mLogMessge = new StringBuilder();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.reg).setOnClickListener(this);
        findViewById(R.id.unreg).setOnClickListener(this);
        mLogView = (TextView) findViewById(R.id.log);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reg:
                addLogMessage("regDataReceiver");
                regDataRecver();
                break;
            case R.id.unreg:
                addLogMessage("unregDataReceiver");
                unRegDataRecver();
                break;
        }
    }

    private void regDataRecver() {
        PassThroughManager.getInstance(this).regDataRecver(new PassThroughManager.PassThroughReceiveDataListener() {
            @Override
            public void onReceivePassThroughResult(int resultCode, String data) {
                addLogMessage("onReceivePassThroughResult {resultCode:" + resultCode + ", data:" + data + "}");
            }
        });
    }

    private void unRegDataRecver() {
        Toast.makeText(getBaseContext(), "Un Reg Data Recver Ok", Toast.LENGTH_LONG).show();
        PassThroughManager.getInstance(this).unRegDataRecver();
    }

    private void addLogMessage(String message) {
        Log.d("MainActivity", message);
        mLogMessge.append(getCurrentTimeStr()).append(" ").append(message).append("\n");
        mLogView.setText(mLogMessge.toString());
    }

    private String getCurrentTimeStr() {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");
        return format.format(new Date());
    }
}
